import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:niuniu/common/constant/AppColor.dart';
import 'package:niuniu/common/widget/LoadingWidget.dart';
import 'package:niuniu/pages/home/view.dart';
import 'package:niuniu/pages/main/controller.dart';
import 'package:niuniu/pages/my/view.dart';
import 'package:niuniu/pages/publish/view.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:get/get.dart';

class MainPage extends StatefulWidget {
   MainPage({Key? key}) : super(key: key);

  @override
  MainPageState createState() => MainPageState();
}

class MainPageState extends State<MainPage> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return  _MainViewGetX();
  }
}

class _MainViewGetX extends GetView<MainController> {
   _MainViewGetX({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    DateTime? lastPressedAt;
    return WillPopScope(
      // 防止连续点击两次退出
      onWillPop: () async {
        if (lastPressedAt == null ||
            DateTime.now().difference(lastPressedAt!) >
                 Duration(seconds: 1)) {
          lastPressedAt = DateTime.now();
          LoadingWidget.toast('再按一次即退出');
          return false;
        }
        await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
        return true;
      },
      child: const Scaffold(
        extendBody: true,
        resizeToAvoidBottomInset: false,
        // 内容页
        body: HomePage()
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainController>(
      init: MainController(),
      id: "main",
      builder: (_) => _buildView(),
    );
  }
}
